<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免费声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use think\admin\Controller;

/**
 * 进出店记录
 * Class User
 * @package app\admin\controller
 */
class StoreInOutLog extends Controller
{

    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'lg_storein_log';

    /**
     * 进出店记录列表
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '进出店记录管理';
        //门店
        $distributor = $this->app->db->table('lg_distributor')->where('status=1')->order('id asc')->column('name','id');
        $this->assign('distributor', $distributor);
        //链接数据库
        //搜索
        $shop_id = input('shop_id');
        $sex = input('sex');
        $status = input('status');
        $age = input('age');
        $enter_time = input('enter_time');
        //$leave_time = input('leave_time');
        $where = [];
        if ($shop_id) {
            $where[] = ['f.shop_id', '=', $shop_id];
        }
        if ($status) {
            $where[] = ['f.status', '=', $status];
        }
        if ($sex) {
            $where[] = ['f.sex', '=', $sex];
        }
        if ($age) {
            $where[] = ['f.age', '=', $age];
        }
        if ($enter_time) {
            $enter_time = explode(' - ', $enter_time);
            $where[] = ['f.enter_time', 'between time', $enter_time];
        }
        // if ($leave_time) {
        //     $leave_time = explode(' - ', $leave_time);
        //     $where[] = ['f.leave_time', 'between time', $leave_time];
        // }
        $query = $this->_query($this->table);
        $query->alias('f')->field('f.*,d.name,e.is_come');
        $query->join('lg_distributor d', 'f.shop_id  = d.id', 'left');
        $query->join('lg_equip e', 'e.equip_id= f.equipment_id and e.channel_id=f.channel_id');
        $query->where($where);
        $query->order('f.id desc')->page();
    }

    /**
     * 处理列表数据   
     * @param $list
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _page_filter(&$list)
    {
        foreach ($list as $k => $v) {
            $list[$k]['sex'] = $v['sex'] == 1 ? '男' : '女';
            $face = $this->app->db->table('lg_storeout_log')->where('face_id', $v['face_id'])->field('enter_time,channel_id')->select()->toArray();
            if (empty($face)) {
                $list[$k]['leave_time'] = 0;
            } else {
                foreach ($face as $kv => $vk) {
                    $next_time = 0;
                    $next_time = strtotime("+1 day", $v['enter_time']);
                    if ($vk['enter_time'] >= $v['enter_time'] && $vk['enter_time'] < $next_time) {
                        $list[$k]['leave_time'] = $vk['enter_time'];
                        $list[$k]['stay_time'] = round(($vk['enter_time'] - $v['enter_time']), 2);
                        break;
                    }
                }
            }
        }
    }

}
